#define DEBUG
#include <cstdio>
#include <ctime>

using namespace std;

const int maxn=100000, maxk=17, inf=0x7ffffff;

class Set {
public:
  int fa[maxn+1], dep[maxn+1];

  void init(int n) {
    for (int i=1; i<=n; i++) {
      fa[i] = i, dep[i] = 0;
    }
  }

  int getFa(int o) {
    if (fa[o]!=o) {
      int t=getFa(fa[o]);
      dep[o] = dep[o]+dep[fa[o]];
      fa[o] = t;
    }
    return fa[o];
  }

  void merge(int f, int s) {
    int x=getFa(f), y=getFa(s);
    if (x!=y) {
      fa[y] = f;
      dep[y] = 1;
    }
  }
} set;

int min(int a, int b) {
  return a<b ? a : b;
}

class Link {
public:
  int f[maxn+1][maxk+1], g[maxn+1][maxk+1], limit[maxn+1];

  void calcLink(int a, int x) {
    for (int i=limit[a]+1; i<=x; i++) {
      if (i-1>=limit[f[a][i-1]]) {
	calcLink(f[a][i-1], i-1);
      }
      f[a][i] = f[f[a][i-1]][i-1];
      g[a][i] = min(g[f[a][i-1]][i-1], g[a][i-1]);
      limit[a] = i;
    }
  }
} link;

int solve(int a, int b) {
  int ret=inf;
  set.getFa(a);
  set.getFa(b);
  for (int i=maxk; i>=0; i--) {
    if (set.dep[a]-(1<<i)>=set.dep[b]) {
      link.calcLink(a, i);
      ret = min(ret, link.g[a][i]);
      a = link.f[a][i];
      set.getFa(a);
    }
  }
  if (a!=b) {
    return 0;
  } else {
    return ret;
  }
}

int read() {
  char c;
  for (c=getchar(); c<'0' || c>'9'; c=getchar());
  int ret=0;
  for (; c>='0' && c<='9'; c=getchar()) {
    ret = ret*10+c-'0';
  }
  return ret;
}

int main() {
  freopen("money.in", "r", stdin);
  freopen("money.out", "w", stdout);

  int n=read(), m=read();

  set.init(n);
  int lastans=0;
  for (int i=1; i<=m; i++) {
    int type=read();

    if (type==0) {
      int a=read(), b=read(), c=read();
      a = (a + lastans) % n + 1;
      b = (b + lastans) % n + 1;
      c = (c + lastans) % n + 1;

      set.merge(b, a);
      link.f[a][0] = b;
      link.g[a][0] = c;
    } else {
      int a=read(), b=read();
      a = (a + lastans) % n + 1;
      b = (b + lastans) % n + 1;

      lastans = solve(a, b);
      printf("%d\n", lastans);
    }
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
